package com.ll.spring5.dao;

import com.ll.spring5.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * 2020/9/11 - 9:20
 */
@Repository
public class BookDaoImpl implements BookDao{

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //添加方法
    @Override
    public void add(Book book) {
        //1.创建sql语句
        String sql = "insert into t_book values(?,?,?)";
        //2.调用方法实现
        Object[] args = {book.getUserId(),book.getUsername(), book.getUstatus()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println("BookDaoImpl=>add(): 成功添加: " + update + " 行");
    }


    //修改
    @Override
    public void updateBook(Book book) {
        String sql = "update t_book set username=?,ustatus=? where user_id=?";
        Object[] args = {book.getUsername(), book.getUstatus(), book.getUserId()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println("BookDaoImpl=>updateBook(): 成功修改: " + update + " 行");
    }


    //删除
    @Override
    public void delete(String id) {
        String sql = "delete from t_book where user_id=?";
        int update = jdbcTemplate.update(sql, id);
        System.out.println("BookDaoImpl=>delete(): 成功删除: " + update + " 行");
    }


    //查询表中的记录数
    @Override
    public int selectCount() {
        String sql = "select count(*) from t_book";

        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);

        return count;
    }

    //查询返回对象
    @Override
    public Book findBookInfo(String id) {
        String sql = "select * from t_book where user_id=?";
        //调用方法
        Book book = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Book>(Book.class), id);
        return book;
    }

    //查询返回集合
    @Override
    public List<Book> findAllBook() {
        String sql = "select * from t_book";
        //调用方法
        List<Book> bookList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
        return bookList;
    }

    //批量添加
    @Override
    public void batchAddBook(List<Object[]> batchArgs) {

        String sql = "insert into t_book values(?,?,?)";

        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);

        System.out.println("BookDaoImpl=>batchAddBook(): " + Arrays.toString(ints));
    }

    //批量修改
    @Override
    public void batchUpdateBook(List<Object[]> batchArgs) {
        String sql = "update t_book set username=?,ustatus=? where user_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println("BookDaoImpl=>batchUpdateBook(): " + Arrays.toString(ints));
    }

    //批量删除
    @Override
    public void batchDeleteeBook(List<Object[]> batchArgs) {
        String sql = "delete from t_book where user_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
        System.out.println("BookDaoImpl=>batchDeleteeBook(): " + Arrays.toString(ints));
    }

}
